package algocasts;

public class P144_BestTimeToBuyAndSellStockII {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) return 0;
        int profit = 0, buyPrice, sellPrice;
        int i = 0, n = prices.length;
        while (i < n - 1) {
            while (i < n - 1 && prices[i + 1] <= prices[i]) ++i;
            buyPrice = prices[i];
            while (i < n - 1 && prices[i + 1] >= prices[i]) ++i;
            sellPrice = prices[i];
            profit += (sellPrice - buyPrice);
        }
        return profit;
    }

    public int maxProfit_Greedy(int[] prices) {
        if (prices == null || prices.length == 0) return 0;
        int profit = 0;
        for (int i = 1; i < prices.length; i++)
            profit += Math.max(prices[i] - prices[i - 1], 0);
        return profit;
    }

}
